﻿////自适应窗口大小
//(function (w, dd, db, p1, p2, p3, p4, p5) {
//    var p;
//    if (!w.size) {
//        (function (_ele, _p) {
//            w.size = function () { return { w: _ele[_p[0]], h: _ele[_p[1]]} }
//        })(typeof w[(p = p3)[0]] == 'number' ? w : dd && (typeof dd[(p = p4)[0]] == 'number') ? dd : db, p)
//    }
//    if (!w.scrollSize) {
//        (function (_ele, _p) {
//            w.scrollSize = function () { return { x: _ele[_p[0]], y: _ele[_p[1]]} };
//        })(typeof w[(p = p1)[0]] == 'number' ? w : dd && (typeof dd[(p = p2)[0]] == 'number') ? dd : db, p)
//    }
//    if (!w.contentSize) {
//        (function (_ele) {
//            w.contentSize = function () { return { w: Math.max(_ele[p5[0]], _ele[p5[2]]), h: Math.max(_ele[p5[1]], _ele[p5[3]])} }
//        })(dd || db)
//    }
//    p = null;
//})(window, document.documentElement, document.body
//        , ['pageXOffset', 'pageYOffset']
//        , ['scrollLeft', 'scrollTop']
//        , ['innerWidth', 'innerHeight']
//        , ['clientWidth', 'clientHeight']
//        , ['scrollWidth', 'scrollHeight', 'offsetWidth', 'offsetHeight']
//    )

//    function fitBody(){
//        var contain = document.getElementById('contain');
//        var h = window.size().h;
//        contain.style.height = Math.max(h - 145, 10) + 'px';
//    }
//    window.onresize = function(){
//        fitBody();
//    }

//给String类添加新的方法trim
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/gi, "");
}

//为String类添加转换date的方法
String.prototype.ToDate = function () {
    return new Date(this.replace(/-/, "/"));
}

//为Date类重写ToString方法  返回的格式  xxxx-xx-xx
Date.prototype.DateToString = function () {
    var year = this.getFullYear()
    var month = parseInt(this.getMonth()) + parseInt("1");
    var day = this.getDate();
    return year + "-" + month + "-" + day;
}

//取从num1到num2的随机数的方法
function SelectFrom(num1, num2) {
    var choices = num2 - num1 + 1;
    return Math.floor(Math.random() * choices + num1);
}

//判断一个对象是存在于实例中还是存在于原型中，如果存在于实例中则返回true，否则返回false
function hasPrototypeProperty(obj, name) {
    if (obj.hasOwnProperty(name) && name in obj) {
        return true;
    }
    else {
        return false;
    }
}

//superType是父类,subType是子类,继承需要用到此方法
function inheritPrototype(subType, superType) {
    var prototype = Object(superType.prototype);
    prototype.constructor = subType;
    subType.prototype = prototype;
}

//只在IE8以上的浏览器有效
HTMLDocument.prototype.test = function () {
    return "测试document的原型重写";
}

//针对在浮点计算中可能出现的浮点误差
//加法
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return (arg1 * m + arg2 * m) / m
}

//减法
function accSub(arg1, arg2) {
    var r1, r2, m, n;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

//乘法
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

//除法
function accDiv(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""))
        r2 = Number(arg2.toString().replace(".", ""))
        return (r1 / r2) * pow(10, t2 - t1);
    }
}

//截取字符串
function cutStr(str, num) {
    var temp = "";
    if (str == "") {
        return "";
    }
    if (str.length > num) {
        temp = str.substring(0, num) + "...";
    }
    else {
        temp = str;
    }
    return temp;
}


//button按下后，不允许按下一次(给一个div层盖住)
function setLoading(obj) {
    var loadingControlID = "divLoadingActive";
    var objCurrent = $(obj);
    var height = objCurrent.height();
    var width = objCurrent.width();
    var offset = objCurrent.offset();
    var left = offset.left;
    var top = offset.top;
    if ($("#" + loadingControlID).length <= 0) {
        $("body").append('<div id=' + loadingControlID + ' style="position:absolute;background-color:gray;text-align:center" >处理中...</div>');
    }
    var objLoadingDiv = $("#" + loadingControlID);
    objLoadingDiv.height(height);
    objLoadingDiv.width(width);
    objLoadingDiv.css("left", left);
    objLoadingDiv.css("top", top);
    return $("#" + loadingControlID);
}

//关于textbox的title处理解决方案
function mouseevent(obj) {
    if ($("#divtitle").length <=0) {
        $("body").append($("<div id='divtitle' style='position:absolute;font-size:12px;width:150px;table-layout:fixed; word-break: break-all; overflow:hidden;'></div>"));
    }
    $(obj).mousemove(function (e) {
        var posx = e.pageX;
        var posy = e.pageY;
        $("#divtitle").css("display", "block");
        $("#divtitle").css("border", "1px solid gray");
        $("#divtitle").css("background-color", "white");
        $("#divtitle").css("top", posy + 20);
        $("#divtitle").css("left", posx + 20);
        if ($(obj).val() == "") {
            $("#divtitle").css("display", "none");
        }
        $("#divtitle").html($(obj).val());
    })
    $(obj).mouseout(function (e) {
        $("#divtitle").css("display", "none");
    })
}

//去除textbox边框
function verview_reinput() {
    var inputs = $("input[type=text]:disabled");
    inputs.each(function (i) {
        var cor = $(this).parents("td").eq(0).css("background-color");
        if (cor != "listecorure") {
            $(this).removeAttr("disabled").prop("readonly", "readonly").css({ "background-color": cor, "border-width": "0 0", "color": "#000000", "cursor": "default" });
        }
        if (i > inputs.lenght) {
            return false;
        }
    });
}

//比较时间大小,格式xxxx-xx-xx   0为相等，1为大于，-1为小于
function comparetime(time1, time2) {
    var date1 = time1.replace(/-/g, "/");
    var date2 = time2.replace(/-/g, "/");
    var d1 = new Date(date1);
    var d2 = new Date(date2);

    if (Date.parse(d1) - Date.parse(d2) == 0) {
        return "0";
    }
    if (Date.parse(d1) - Date.parse(d2) < 0) {
        return "-1";
    }
    if (Date.parse(d1) - Date.parse(d2) > 0) {
        return "1";
    }
}

//当前输入时间转换成星期几格式  2012-8-25
function toDay(str) {
    var arr = "日一二三四五六".split("");
    vYear = str.split('-')[0];
    vMonth = str.split('-')[1];
    vDay = str.split('-')[2];
    return "星期" + arr[new Date(vYear, vMonth - 1, vDay).getDay()];
}

//去除数组里的重复项 返回新的数组
Array.prototype.unique = function () {
    var temp = new Array();
    this.sort();
    for (i = 0; i < this.length; i++) {
        if (this[i] == this[i + 1]) {
            continue;
        }
        temp[temp.length] = this[i];
    }
    return temp;
}

//判断某项在数组里是否存在  true代表存在 false代表不存在
Array.prototype.isExist = function (val) {
    var bl = false;
    for (var i = 0; i < this.length; i++) {
        if (val == this[i]) {
            bl = true;
        }
    }
    return bl;
}

//js删除数组里的index项  索引从0开始  返回新的数组
Array.prototype.delByIndex = function (index) {
    if (index < 0) {
        return this;
    }
    else {
        return this.slice(0, index).concat(this.slice(index + 1, this.length));
    }
    /*
　　　    concat方法：返回一个新数组，这个新数组是由两个或更多数组组合而成的。
　　　　　　　　　    这里就是返回this.slice(0,n)/this.slice(n+1,this.length)
　　 　　　　　　    组成的新数组，这中间，刚好少了第n项。
　　　    slice方法： 返回一个数组的一段，两个参数，分别指定开始和结束的位置。
    */
}

//字符串过长的换行问题---主要用于div的tooltip
function DealWithToolTipString(str, strcutlength) {
    if (str.length < strcutlength) {
        return str;
    }

    var length = str.length;
    var count = length / strcutlength;
    var temparr = new Array();

    if (count.toString().indexOf('.') == -1) {
        count = count;
        for (var i = 0; i < count; i++) {
            temparr.push(str.substring(i * strcutlength, (i + 1) * strcutlength));
        }
    }
    else {
        count = parseInt(count) + 1;
        for (var i = 0; i < count; i++) {
            temparr.push(str.substring(i * strcutlength, (i + 1) * strcutlength));
        }
    }

    var result = "";
    for (var i = 0; i < temparr.length; i++) {
        result += temparr[i] + "<br/>";
    }
    return result;
}

//时间转换
function ChangeDate(val) {
    var res = "";
    if (val == null || val == "null") {
        res = "";
    } else {
        var d = new Date(val);
        d = d.toLocaleDateString();
        d = d.replace('年', '-').replace('月', '-').replace('日', '');
        var arr = d.split('-');
        var yy = arr[0];
        var mm = arr[1];
        var dd = arr[2];
        if (parseInt(mm) < 10) {
            mm = "0" + mm;
        }
        if (parseInt(dd) < 10) {
            dd = "0" + dd;
        }
        res = yy + "-" + mm + "-" + dd;
    }
    return res;
}

//时间转换 格式 2012-9-9 00:00:00
function ChangeDateByDate(val) {
    if (val == "") {
        return;
    } else {
        return val.split(' ')[0];
    }
}

//封装的ajax方法
function SendAjax(reqtype, requrl, reqdata,datatype,callback) {
    $.ajax({
        type: reqtype,
        async: true,
        url: requrl,
        dataType: datatype == "" ? "text" : datatype,
        data: reqdata,
        cache: false,
        success: callback,
        error: function (jqXHR, textStatus, errorThrown) {
            alert("HTTP请求错误代码:" + jqXHR.status + ",描述:" + jqXHR.statusText);
        }

    });
}

//自定义的弹出层,需要引入jq jqui 
var CusDialog = {
    showdialog: function (title, content, callback) {
        var html = "<div id='divconfirm' style='display: block; z-index: 1007; outline: 0px none; height: auto; width: 400px; top: 107.5px; left: 519px;' class='ui-dialog ui-widget ui-widget-content ui-corner-all confirm-warn ui-draggable' tabindex='-1' role='dialog' aria-labelledby='ui-dialog-title-1'><div class='ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix'><span class='ui-dialog-title' id='ui-dialog-title-1'>" + title + "&nbsp;</span><a href='#' class='ui-dialog-titlebar-close ui-corner-all' role='button'><span id='divdialogClose' class='ui-icon ui-icon-closethick' onclick='divdialoghide();'>close</span></a></div><div class='ui-dialog-content ui-widget-content' style='width: auto; min-height: 38px; height: auto;' scrolltop='0' scrollleft='0'><div class='dialog-del-status'>" + content + "</div></div><div class='ui-dialog-buttonpane ui-widget-content ui-helper-clearfix'><div class='ui-dialog-buttonset'><button type='button' class='btn-sure' id='divdialogAjaxSend'>确    定</button><button type='button' class='btn-cancel' id='divcancel'>取 消</button></div></div></div>";
        $("body").append($(html));
        $("#divdialogAjaxSend").click(function () {
            callback();
            $("#divconfirm").remove();
        })

        $("#divdialogClose").click(function () {
            $("#divconfirm").remove();
        })

        $("#divcancel").click(function () {
            $("#divconfirm").remove();
        })
    }
}